package org.liam.ltrans;

import cn.hutool.core.io.FileUtil;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.stage.DirectoryChooser;
import org.liam.ltrans.pojo.Ltrans;
import org.liam.ltrans.stage.ProgressStage;
import org.liam.ltrans.util.LtransUtils;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class HelloController {

    @FXML
    private StackPane waitPane;

    @FXML
    private TextField srcPath;

    @FXML
    private TextField targetPath;


    @FXML
    protected void chooseFileSrc() {
        DirectoryChooser chooser = new DirectoryChooser();
        chooser.setTitle("请选择待处理的CSV文件目录：");
        File file = chooser.showDialog(null);
        String absolutePath = file.getAbsolutePath();
        List<String> strings = FileUtil.listFileNames(absolutePath);
        if (strings.size() != 0) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setContentText("检测到指定目录下存在非预期的文件，请确保文件目录存在并满足处理标准");
            alert.show();
        }

        srcPath.setText(absolutePath);
    }


    @FXML
    protected void chooseFileTarget() {
        DirectoryChooser chooser = new DirectoryChooser();
        chooser.setTitle("请选择文件保存的目录：");
        File file = chooser.showDialog(null);
        String absolutePath = file.getAbsolutePath();
        if (absolutePath.equals(srcPath.getText())) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setContentText("文件不能被输出在数据目录下，请选择一个其他的目录输出");
            alert.show();
        }

        targetPath.setText(absolutePath);
    }

    @FXML
    protected void doWork() throws UnsupportedEncodingException {
        String tar = targetPath.getText();
        String srcPathText = srcPath.getText();
        if (tar.equals(srcPathText)) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setContentText("文件不能被输出在数据目录下，请选择一个其他的目录输出");
            alert.show();
        }
        List<String> strings = FileUtil.listFileNames(srcPathText);
        if (strings.size() != 0) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setContentText("检测到指定目录下存在非预期的文件，请确保文件目录存在并满足处理标准");
            alert.show();
        }
        ProgressStage progressStage = new ProgressStage();
        progressStage.initUI("Loading...");
        progressStage.show();

        LtransUtils utils = new LtransUtils();
        // 定义返回体
        List<Ltrans> ltrans = new ArrayList<>();
        //获取全部的目录名称（排序后）
        String[] sordedDirName = utils.sortDirectories(srcPathText);
        for (String dirName : sordedDirName) {
            utils.dealWithBatchLevel(srcPathText, dirName, ltrans);
        }
        List<List<String>> exportExcelData = utils.getExportExcelData(ltrans);
        String tarfilePath = tar + File.separator + "lrtans.csv";
        utils.exportCSV(exportExcelData, tarfilePath);
        progressStage.close();
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setContentText("文件已保存在： " + tarfilePath);
        alert.show();

    }

    @FXML
    protected void openTarDir() throws IOException {
        String tar = targetPath.getText();
        FileUtil.mkdir(tar);
        Desktop.getDesktop().open(new File(tar));

    }


}
